#!/bin/bash

# OpenTelemetry Performance Tests Runner
# 运行所有性能测试的脚本

echo "🚀 OpenTelemetry Performance Tests Runner"
echo "=========================================="

# 检查是否在正确的目录
if [ ! -f "install/setup.bash" ]; then
    echo "❌ 错误: 请在ROS2工作空间根目录运行此脚本"
    echo "   当前目录: $(pwd)"
    echo "   请确保在包含install/setup.bash的目录中运行"
    exit 1
fi

# 编译项目
echo "📦 编译项目..."
colcon build --packages-select opentelemetry_demo
if [ $? -ne 0 ]; then
    echo "❌ 编译失败"
    exit 1
fi

# 设置环境
echo "🔧 设置环境..."
source install/setup.bash

# 测试列表
tests=(
    "basic_span_test:基础Span创建测试"
    "high_frequency_test:高频Span测试"
    "large_attribute_test:大属性测试"
    "concurrent_test:并发测试"
    "nesting_test:深度嵌套测试"
    "memory_stress_test:内存压力测试"
)

echo ""
echo "🧪 开始运行性能测试..."
echo ""

# 运行每个测试
for test_info in "${tests[@]}"; do
    IFS=':' read -r test_name test_desc <<< "$test_info"
    
    echo "=========================================="
    echo "🔍 运行测试: $test_desc ($test_name)"
    echo "=========================================="
    
    # 运行测试
    timeout 60 ros2 run opentelemetry_demo $test_name
    
    if [ $? -eq 0 ]; then
        echo "✅ $test_desc 完成"
    elif [ $? -eq 124 ]; then
        echo "⏰ $test_desc 超时 (60秒)"
    else
        echo "❌ $test_desc 失败"
    fi
    
    echo ""
    echo "⏳ 等待3秒后继续下一个测试..."
    sleep 3
    echo ""
done

echo "=========================================="
echo "🎉 所有性能测试完成!"
echo "=========================================="
echo ""
echo "📊 测试总结:"
echo "- 基础Span创建测试: 测试单个span的基础开销"
echo "- 高频Span测试: 测试高频率span创建性能"
echo "- 大属性测试: 测试大量属性对性能的影响"
echo "- 并发测试: 测试多线程环境下的性能"
echo "- 深度嵌套测试: 测试span嵌套对性能的影响"
echo "- 内存压力测试: 测试内存使用情况"
echo ""
echo "💡 提示: 查看上面的输出结果，分析OpenTelemetry的性能特征"
echo "📁 详细说明请查看: src/opentelemetry_demo/performance_tests_summary.md"
